summaryrefslogtreecommitdiff
path: root/app/[lng]/test/table-v2/actions.ts
blob: f5fd5f6646bd16429f9949cfb0f2ac33bbdfc2ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"use server";

import db from "@/db/db";
import { testProducts, testOrders, testCustomers } from "@/db/schema/test-table-v2";
import { createTableService } from "@/components/client-table-v2/adapter/create-table-service";
import { DrizzleTableState } from "@/components/client-table-v2/adapter/drizzle-table-adapter";
import { productColumnDefs, OrderWithDetails, ServerColumnMeta } from "./column-defs";
import { SQL, count, eq, desc, sql, asc } from "drizzle-orm";
import { TestProduct } from "@/db/schema/test-table-v2";

// ============================================================
// Pattern 1: Client-Side - 전체 데이터 로드
// ============================================================

export async function getAllProducts() {
  return await db.select().from(testProducts).orderBy(testProducts.id);
}

// ============================================================
// Pattern 2: Factory Service - 자동 생성된 서버 액션
// ============================================================

// Server-side용 컬럼 정의 사용 (React 컴포넌트 없음)
export const getProductTableData = createTableService({
  db,
  schema: testProducts,
  columns: productColumnDefs,
});

// ============================================================
// Pattern 2-B: Factory Service with Grouping Support
// ============================================================

/**
 * 그룹 정보 타입
 */
export interface GroupInfo {
  groupKey: string;
  groupValue: string | number | boolean | null;
  count: number;
  // 확장 시 로드된 하위 행들
  rows?: TestProduct[];
}

/**
 * 그룹핑 응답 타입
 */
export interface GroupedResponse {
  groups: GroupInfo[];
  totalGroups: number;
}

/**
 * 일반 응답 타입
 */
export interface NormalResponse {
  data: TestProduct[];
  totalRows: number;
  pageCount: number;
}

/**
 * 서버 사이드 그룹핑을 지원하는 상품 테이블 데이터 조회
 * 
 * @param tableState - 테이블 상태 (pagination, sorting, filters, grouping)
 * @param expandedGroups - 확장된 그룹 키 목록 (예: ["category:Electronics", "status:active"])
 */
export async function getProductTableDataWithGrouping(
  tableState: DrizzleTableState,
  expandedGroups: string[] = []
): Promise<GroupedResponse | NormalResponse> {
  const { grouping, pagination } = tableState;
  
  // 그룹핑이 없으면 일반 조회
  if (!grouping || grouping.length === 0) {
    const result = await getProductTableData(tableState);
    return result as NormalResponse;
  }

  // 첫 번째 그룹핑 컬럼만 처리 (다중 그룹핑은 복잡도가 높음)
  const groupColumnId = grouping[0];
  
  // 서버 그룹핑 가능 여부 확인
  const columnDef = productColumnDefs.find(
    col => 'accessorKey' in col && col.accessorKey === groupColumnId
  );
  const meta = columnDef?.meta as ServerColumnMeta | undefined;
  
  if (!meta?.serverGroupable) {
    // 서버 그룹핑 불가 - 전체 데이터 반환하여 클라이언트에서 처리
    console.warn(`Column "${groupColumnId}" does not support server grouping. Falling back to client-side.`);
    const allData = await db.select().from(testProducts);
    return {
      data: allData,
      totalRows: allData.length,
      pageCount: 1,
    };
  }

  // 그룹별 카운트 조회
  const groupColumn = getProductColumn(groupColumnId);
  if (!groupColumn) {
    throw new Error(`Unknown column: ${groupColumnId}`);
  }

  const groupsResult = await db
    .select({
      groupValue: groupColumn,
      count: count(),
    })
    .from(testProducts)
    .groupBy(groupColumn)
    .orderBy(asc(groupColumn));

  // 그룹 정보 구성
  const groups: GroupInfo[] = await Promise.all(
    groupsResult.map(async (g) => {
      const groupKey = `${groupColumnId}:${g.groupValue}`;
      const isExpanded = expandedGroups.includes(groupKey);
      
      let rows: TestProduct[] | undefined;
      
      // 확장된 그룹의 하위 행 로드
      if (isExpanded) {
        rows = await db
          .select()
          .from(testProducts)
          .where(eq(groupColumn, g.groupValue))
          .orderBy(testProducts.id)
          .limit(pagination?.pageSize ?? 100); // 그룹 내 행 제한
      }

      return {
        groupKey,
        groupValue: g.groupValue,
        count: Number(g.count),
        rows,
      };
    })
  );

  return {
    groups,
    totalGroups: groups.length,
  };
}

/**
 * 컬럼 ID로 Drizzle 컬럼 객체 반환
 */
function getProductColumn(columnId: string) {
  const columnMap: Record<string, any> = {
    id: testProducts.id,
    sku: testProducts.sku,
    name: testProducts.name,
    category: testProducts.category,
    price: testProducts.price,
    stock: testProducts.stock,
    status: testProducts.status,
    isNew: testProducts.isNew,
    createdAt: testProducts.createdAt,
    updatedAt: testProducts.updatedAt,
  };
  return columnMap[columnId];
}

// ============================================================
// Pattern 3: Custom Service - 복잡한 조인 쿼리
// ============================================================

export async function getOrderTableData(tableState: DrizzleTableState): Promise<{
  data: OrderWithDetails[];
  totalRows: number;
  pageCount: number;
}> {
  // Pattern 3에서는 DrizzleTableAdapter를 사용하지 않습니다.
  // 조인된 결과의 컬럼들은 단일 테이블에 매핑되지 않기 때문입니다.
  // 대신, 페이지네이션 값만 직접 계산합니다.
  
  const pageSize = tableState.pagination?.pageSize ?? 10;
  const pageIndex = tableState.pagination?.pageIndex ?? 0;
  const limit = pageSize;
  const offset = pageIndex * pageSize;

  // Build ORDER BY clause based on sorting state
  const orderByClauses =
    tableState.sorting?.reduce<SQL<unknown>[]>((clauses, sort) => {
      const columnMap: Record<string, any> = {
        id: testOrders.id,
        orderNumber: testOrders.orderNumber,
        quantity: testOrders.quantity,
        unitPrice: testOrders.unitPrice,
        totalAmount: testOrders.totalAmount,
        status: testOrders.status,
        orderedAt: testOrders.orderedAt,
        customerName: testCustomers.name,
        customerEmail: testCustomers.email,
        customerTier: testCustomers.tier,
        productName: testProducts.name,
        productSku: testProducts.sku,
      };

      const column = columnMap[sort.id];
      if (!column) return clauses;

      clauses.push(sort.desc ? desc(column) : asc(column));
      return clauses;
    }, []) ?? [];

  // 커스텀 조인 쿼리 작성
  const data = await db
    .select({
      id: testOrders.id,
      orderNumber: testOrders.orderNumber,
      quantity: testOrders.quantity,
      unitPrice: testOrders.unitPrice,
      totalAmount: testOrders.totalAmount,
      status: testOrders.status,
      orderedAt: testOrders.orderedAt,
      // 고객 정보 조인
      customerName: testCustomers.name,
      customerEmail: testCustomers.email,
      customerTier: testCustomers.tier,
      // 상품 정보 조인
      productName: testProducts.name,
      productSku: testProducts.sku,
    })
    .from(testOrders)
    .leftJoin(testCustomers, eq(testOrders.customerId, testCustomers.id))
    .leftJoin(testProducts, eq(testOrders.productId, testProducts.id))
    .orderBy(...(orderByClauses.length > 0 ? orderByClauses : [desc(testOrders.orderedAt)]))
    .limit(limit)
    .offset(offset);

  // 총 개수 쿼리
  const totalResult = await db
    .select({ count: count() })
    .from(testOrders);

  const totalRows = Number(totalResult[0]?.count ?? 0);

  return {
    data: data as OrderWithDetails[],
    totalRows,
    pageCount: Math.ceil(totalRows / pageSize),
  };
}

// ============================================================
// Pattern 3-B: Custom Service with Grouping (Orders by Status)
// ============================================================

export interface OrderGroupInfo {
  groupKey: string;
  groupValue: string;
  count: number;
  totalAmount: number;
  rows?: OrderWithDetails[];
}

/**
 * 주문 데이터를 상태별로 그룹핑하여 조회
 */
export async function getOrderTableDataGroupedByStatus(
  expandedGroups: string[] = []
): Promise<{ groups: OrderGroupInfo[]; totalGroups: number }> {
  // 상태별 그룹 집계
  const groupsResult = await db
    .select({
      status: testOrders.status,
      count: count(),
      totalAmount: sql<number>`SUM(${testOrders.totalAmount}::numeric)`,
    })
    .from(testOrders)
    .groupBy(testOrders.status)
    .orderBy(testOrders.status);

  const groups: OrderGroupInfo[] = await Promise.all(
    groupsResult.map(async (g) => {
      const groupKey = `status:${g.status}`;
      const isExpanded = expandedGroups.includes(groupKey);
      
      let rows: OrderWithDetails[] | undefined;
      
      if (isExpanded) {
        // 확장된 그룹의 상세 주문 조회 (조인 포함)
        const orderRows = await db
          .select({
            id: testOrders.id,
            orderNumber: testOrders.orderNumber,
            quantity: testOrders.quantity,
            unitPrice: testOrders.unitPrice,
            totalAmount: testOrders.totalAmount,
            status: testOrders.status,
            orderedAt: testOrders.orderedAt,
            customerName: testCustomers.name,
            customerEmail: testCustomers.email,
            customerTier: testCustomers.tier,
            productName: testProducts.name,
            productSku: testProducts.sku,
          })
          .from(testOrders)
          .leftJoin(testCustomers, eq(testOrders.customerId, testCustomers.id))
          .leftJoin(testProducts, eq(testOrders.productId, testProducts.id))
          .where(eq(testOrders.status, g.status))
          .orderBy(desc(testOrders.orderedAt))
          .limit(50);
        
        rows = orderRows as OrderWithDetails[];
      }

      return {
        groupKey,
        groupValue: g.status,
        count: Number(g.count),
        totalAmount: Number(g.totalAmount) || 0,
        rows,
      };
    })
  );

  return {
    groups,
    totalGroups: groups.length,
  };
}